/**
 *
 *embustest message process.
 *
 * @msgproctest.cpp
 *
 * @remarks
 * 2022 created by yy.
 */

#include "msgproctest.h"

using namespace std;

static uint64_t g_rx_count = 0;
static uint64_t g_rx_count_can = 0;
static uint64_t g_rx_count_lin = 0;
static uint64_t g_tx_count_can = 0;
static uint64_t g_tx_count_lin = 0;

//define uint64_t key in map
//bit0~bit31: canid;
//bit32~bit39: channel
//bit40: 0---can, 1--canfd
//bit41: 0---rx, 1---tx

std::map<uint64_t,struct log_packet> rxframe_map_;
std::map<uint64_t,struct log_packet> txframe_map_;

char time_str[128] = {0};
struct com_packet s_txcan_frame[32] = {0};

struct state
{
    uint8_t ignState; //1-Ign on; 0-Ign off; 0xff-unGet
};
struct state g_state = {.ignState = 0xff};
/** 
******************************************************************************** 
 * @name StringtoHex
 * @deprecated String to hex form
 * @param str [in]  String before conversion.
 * @param out [out] Converted string. 
 * @return          Converted length
 * @remarks Important: . 
 ******************************************************************************** 
*/
int StringtoHex(char *str, unsigned char *out)
{
    int i = 0, ret = 0;
    if (str == NULL || out == NULL)
        return -1;
    ret = (strlen(str) / (2 * sizeof(char))) + strlen(str) % (2 * sizeof(char));
    for (i = 0; i < ret; i++)
        sscanf(str + 2 * i, "%02X", (unsigned int *)(out + i));
 
    return ret;
}

/** 
******************************************************************************** 
 * @name get_format_time_st
 * @deprecated Timestamp format conversion
 * @param timestamp [in] Timestamp form before conversion (DEC).
 * @param time_s    [out] Timestamp after conversion (year-month-day hour:minute:second.xxxxxx). 
 * @return 
 * @remarks Important: . 
 ******************************************************************************** 
*/
void get_format_time_st(uint64_t timestamp, char *time_s)
{
    char tmp[10];
    struct tm *tm_t;
    unsigned long us = timestamp % 1000000;
    timestamp = timestamp / 1000000;
    time_t t_stamp = (time_t)timestamp;
    //tm_t = localtime(&t_stamp);
    tm_t = gmtime(&t_stamp);
    strftime(time_str, 128, "%Y-%m-%d %H:%M:%S", tm_t);               
    sprintf(tmp, ".%06ld", us);
    strcat(time_s, tmp);
}

/** 
******************************************************************************** 
 * @name  print_can_com_frame_info
 * @deprecated Print log_packet content
 * @param pac       [in] log_packet* pacinfo --> log_packet struct.
 * @return   NA
 * @remarks  Important: . 
 ******************************************************************************** 
*/
void print_can_com_frame_info(log_packet* pacinfo)
{  
    char msg_flags[8];
    char msg_dir[3];
    char msg_type[10];
    struct com_packet* pac = &(pacinfo->frame);
    
    /* rx, tx count */
    if (((pac->channel & 0x20) >> 5) == 0) {
        strcpy(msg_dir, "rx");
    }          
    else {
        strcpy(msg_dir, "tx");
    }    
    if (((pac->channel & 0xC0) >> 6) == 0x00) {
        strcpy(msg_type, "can");
        strcpy(msg_flags, "NA"); 
    }
    else {
        strcpy(msg_type, "canfd"); 
        if (pac->flag == 0x01) strcpy(msg_flags, "ESI"); 
        else if (pac->flag == 0x02) strcpy(msg_flags, "BRS"); 
        else if (pac->flag == 0x03) strcpy(msg_flags, "ESI|BRS");    
        else {
            strcpy(msg_flags, "NA"); 
        }  
    }    
    
    /* Print the specified channel can message data. */
    //time_t t_stamp = (time_t)pac->timestamp;
    time_t t_stamp;
    time(&t_stamp);
    //get_format_time_st(t_stamp, (char *)time_str);
    struct tm* tm_t = localtime(&t_stamp);
    strftime(time_str, 128, "%Y-%m-%d %H:%M:%S", tm_t);     
    if (strcmp(msg_dir, "rx") == 0)
    {
        printf("%lu\t\t%s\t0x%x\t%u\t%s\t%s\t%s\t[%hu]\t",
            pacinfo->count, time_str, pac->id /*& 0x0FFFFFFF)*/, (pac->channel & 0x0F), msg_dir, msg_type, msg_flags, pac->dlc);
        for(int i=0; i<pac->dlc; ++i)
        {
            printf("%02X ", pac->data[i]);
        }
        printf("\n");   
    }
    else
    {
        printf("%lu\t\t%s\t0x%x\t%u\t%s\t%s\t%s\t[%hu]\t",
            pacinfo->count, time_str, pac->id /*& 0x0FFFFFFF)*/, (pac->channel & 0x0F), msg_dir, msg_type, msg_flags, pac->dlc);
        for(int i=0; i<pac->dlc; ++i)
        {
            printf("%02X ", pac->data[i]);
        }
        printf("\n");    
    }
}
/** 
******************************************************************************** 
 * @name  update_can_com_framemap_info
 * @deprecated Print rxframe_map_/txframe_map_ content
 * @param pac       [in] struct com_packet* --> com_packet struct.
 * @return   NA
 * @remarks  Important: . 
 ******************************************************************************** 
*/
void update_can_com_framemap_info(struct com_packet* pac)
{
    struct log_packet packetinfo = {0};

    //define uint64_t key in map
    //bit0~bit31: canid;
    //bit32~bit39: channel
    //bit40: 0---can, 1--canfd
    //bit41: 0---rx, 1---tx
    uint64_t key = pac->id;

    uint64_t ch =  pac->channel;
    key += (ch << 32);

    /* rx, tx count */
    if (((pac->channel & 0x20) >> 5) != 0) { //tx
        auto iter = txframe_map_.find(key);
        if (iter != txframe_map_.end())
        {
            iter->second.count += 1;
            if (memcmp(iter->second.frame.data,pac->data,pac->dlc) != 0)
            {
                memcpy(iter->second.frame.data,pac->data,pac->dlc);
            }
            return;
        } 
        packetinfo.count = 1;
        memcpy(&packetinfo.frame,pac,sizeof(struct com_packet));
        txframe_map_.insert(pair<uint64_t, struct log_packet>(key, packetinfo));
        //printf("canid = %lx, key = %lx\n",pac->id,key);
    }
    else 
    {
        auto iter = rxframe_map_.find(key);
        if (iter != rxframe_map_.end())
        {
            iter->second.count += 1;
            if (memcmp(iter->second.frame.data,pac->data,pac->dlc) != 0)
            {
                memcpy(iter->second.frame.data,pac->data,pac->dlc);
            }
            return;
        } 
        packetinfo.count = 1;
        memcpy(&packetinfo.frame,pac,sizeof(struct com_packet));
        rxframe_map_.insert(pair<uint64_t, struct log_packet>(key, packetinfo));
    }
}
/** 
******************************************************************************** 
 * @name  show_can_com_framemap_info
 * @deprecated Print rxframe_map_/txframe_map_ content
 * @return   NA
 * @remarks  Important: . 
 ******************************************************************************** 
*/
void show_can_com_framemap_info()
{
    uint64_t rxcount = 0;
    uint64_t txcount = 0;
    if (rxframe_map_.empty() && txframe_map_.empty())
        return;

    printf ("\033c");
    printf("count\t\ttimestamp\t\tcanid\tchannel\tdir\ttype\tflag\tdlc\tdata\n");
    for(auto &rxpac : rxframe_map_)
    {
        print_can_com_frame_info(&rxpac.second);
        rxcount += rxpac.second.count;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    if (!rxframe_map_.empty())
    {
        printf("rx total number = %lu\n",rxcount);
    }
    for(auto &txpac : txframe_map_)
    {
        print_can_com_frame_info(&txpac.second);
        txcount += txpac.second.count;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    if (!txframe_map_.empty())
    {
        printf("tx total number = %lu\n",txcount);
    }
}

/** 
******************************************************************************** 
 * @name  show_can_com_frame_msg
 * @deprecated Print can/canfd message data
 * @param pac       [in] struct com_packet* --> com_packet struct.
 * @param g_channel [in] Send messages to the specified channel.
 * @param fp        [in] Log file.
 * @return   NA
 * @remarks  Important: . 
 ******************************************************************************** 
*/
void show_can_com_frame_msg(struct com_packet* pac, string g_channel, FILE *fp)
{
    uint8_t ch_up = 0;
    uint8_t ch_low = 0;
    char msg_flags[8];
    char msg_dir[3];
    char msg_type[10];
    
    /* rx, tx count */
    if (((pac->channel & 0x20) >> 5) == 0) {
        strcpy(msg_dir, "rx");
        ++g_rx_count_can;  
    }          
    else {
        strcpy(msg_dir, "tx");
        ++g_tx_count_can;
    }    
    if (((pac->channel & 0xC0) >> 6) == 0x00) {
        strcpy(msg_type, "can");
        strcpy(msg_flags, "NA"); 
    }
    else {
        strcpy(msg_type, "canfd"); 
        if (pac->flag == 0x01) strcpy(msg_flags, "ESI"); 
        else if (pac->flag == 0x02) strcpy(msg_flags, "BRS"); 
        else if (pac->flag == 0x03) strcpy(msg_flags, "ESI|BRS");    
        else {
            strcpy(msg_flags, "NA"); 
        }  
    }    
    /* channel 1~16 , get ch_up and ch_low value. */ 
    char strch[64] = {0};
    strcpy(strch, g_channel.c_str()); 
    sscanf(strch, "%hhu~%hhu", &ch_low, &ch_up);
    if (ch_up == 0) ch_up = ch_low; 
    for (uint8_t ch = ch_low; ch <= ch_up; ++ch)
    {
        /* Print the specified channel can message data. */
        if (((pac->channel & 0x0F) == ch) || ((ch_low == 0) && (ch_up == 0 )))
        {
            time_t t_stamp = (time_t)pac->timestamp;
            get_format_time_st(t_stamp, (char *)time_str);
            if (strcmp(msg_dir, "rx") == 0)
            {

#if LOGGING_FLAG
                fprintf(fp, "%lu\t%s\t0x%x\t%u\t%s\t%s\t%s\t[%hu]\t",
                    g_rx_count_can, time_str, (pac->id & 0x0FFFFFFF), (pac->channel & 0x0F), msg_dir, msg_type, msg_flags, pac->dlc); /* logging */
#else
                printf("%lu\t%s\t0x%x\t%u\t%s\t%s\t%s\t[%hu]\t",
                    g_rx_count_can, time_str, (pac->id & 0x0FFFFFFF), (pac->channel & 0x0F), msg_dir, msg_type, msg_flags, pac->dlc);
#endif /* LOGGING_FLAG */
                for(int i=0; i<pac->dlc; ++i)
                {

#if LOGGING_FLAG
                    fprintf(fp, "%02X ", pac->data[i]); /* logging */
#else
                    printf("%02X ", pac->data[i]);
#endif /* LOGGING_FLAG */ 
                }
#if LOGGING_FLAG
                fprintf(fp, "\n"); /* logging */  
#else
                printf("\n");   
#endif /* LOGGING_FLAG */ 
            }
            else
            {
#if LOGGING_FLAG
                fprintf(fp, "%lu\t%s\t0x%x\t%u\t%s\t%s\t%s\t[%hu]\t",
                    g_tx_count_can, time_str, (pac->id & 0x0FFFFFFF), (pac->channel & 0x0F), msg_dir, msg_type, msg_flags, pac->dlc); /* logging */
#else
                printf("%lu\t%s\t0x%x\t%u\t%s\t%s\t%s\t[%hu]\t",
                    g_tx_count_can, time_str, (pac->id & 0x0FFFFFFF), (pac->channel & 0x0F), msg_dir, msg_type, msg_flags, pac->dlc);
#endif /* LOGGING_FLAG */ 
                for(int i=0; i<pac->dlc; ++i)
                {
#if LOGGING_FLAG
                    fprintf(fp, "%02X ", pac->data[i]); /* logging */
#else
                    printf("%02X ", pac->data[i]);
#endif /* LOGGING_FLAG */ 
                }
#if LOGGING_FLAG
                fprintf(fp, "\n");  /* logging */ 
#else
                printf("\n");
#endif /* LOGGING_FLAG */      
            }
        }
    }
}

/** 
******************************************************************************** 
 * @name  show_lin_com_frame_msg
 * @deprecated print lin message data
 * @param pac       [in] struct com_packet* --> com_packet struct.
 * @param g_channel [in] Send message to specified channel.
 * @param fp        [in] Log file.
 * @return   NA
 * @remarks  Important: . 
 ******************************************************************************** 
*/
void show_lin_com_frame_msg(struct com_packet* pac, string g_channel, FILE *fp)
{
    uint8_t ch_up = 0;
    uint8_t ch_low = 0;
    char msg_dir[3];
    char msg_type[10] = "lin";
    char msg_flags[8] = "NA";
    if (((pac->channel & 0x20) >> 5) == 0) {
        strcpy(msg_dir, "rx");  
        ++g_rx_count_lin;
    }          
    else {
        strcpy(msg_dir, "tx"); 
        ++g_tx_count_lin;
    }          
    /* channel 1~16 , get ch_up and ch_low value. */ 
    char strch[64] = {0};
    strcpy(strch, g_channel.c_str()); 
    sscanf(strch, "%hhu~%hhu", &ch_low, &ch_up);
    if (ch_up == 0) ch_up = ch_low;  
    for (uint8_t ch = ch_low; ch <= ch_up; ++ch)
    {
        /* Print the specified channel can message data. */
        if (((pac->channel & 0x0F) == ch) || ((ch_low == 0) && (ch_up == 0 )))
        {
            time_t t_stamp = (time_t)pac->timestamp;
            get_format_time_st(t_stamp, (char *)time_str);
            printf("%lu\t%s\t0x%x\t%u\t%s\t%s\t%s\t[%hu]\t",
                g_rx_count_lin, time_str, (pac->id & 0x0FFFFFFF), (pac->channel & 0x0F), msg_dir, msg_type, msg_flags, pac->dlc);
#if LOGGING_FLAG
            fprintf(fp, "%lu\t%s\t0x%x\t%u\t%s\t%s\t%s\t[%hu]\t",
                g_rx_count_lin, time_str, (pac->id & 0x0FFFFFFF), (pac->channel & 0x0F), msg_dir, msg_type, msg_flags, pac->dlc);
#endif /* LOGGING_FLAG */ 
            for(int i=0; i<pac->dlc; ++i)
            {
                printf("%02X ", pac->data[i]);
#if LOGGING_FLAG
                fprintf(fp, "%02X ", pac->data[i]);
#endif /* LOGGING_FLAG */ 
            }
            printf("\n");
#if LOGGING_FLAG
            fprintf(fp, "\n");
#endif /* LOGGING_FLAG */ 
        }
    }
}

/** 
******************************************************************************** 
 * @name  show_vio_status_msg
 * @deprecated print vio status message data
 * @param pac       [in] struct com_packet* --> com_packet struct.
 * @param g_channel [in] Send message to specified channel.
 * @return   NA
 * @remarks  Important: . 
 ******************************************************************************** 
*/
void show_vio_status_msg(struct vio_status* pac, string g_channel)
{
    uint8_t ch_up = 0;
    uint8_t ch_low = 0;
    int16_t gm_3v8_adc;
    int16_t sys_2v5_adc;
    int16_t doip_line_volt;
    int16_t hs_tempreture;
    gm_3v8_adc      = pac->data[0];
    sys_2v5_adc     = pac->data[1];
    doip_line_volt  = pac->data[2];
    hs_tempreture   = pac->data[3];

    time_t t_stamp = (time_t)pac->timestamp;
    get_format_time_st(t_stamp, (char *)time_str);
    /* channel 1~16 , get ch_up and ch_low value. */ 
    sscanf(g_channel.c_str(), "%hhu~%hhu", &ch_low, &ch_up);
    if (ch_up == 0) ch_up = ch_low;     
    for (uint8_t ch = ch_low; ch <= ch_up; ++ch)
    {
        if (((pac->channel & 0x0F) == ch) || ((ch_low == 0) && (ch_up == 0 )))
        {
            printf("%lu\t%s\t%hhu\t%hd\t%hd\t%hd\t%hd\n", 
                ++g_rx_count, time_str, (pac->channel & 0x0F), gm_3v8_adc, sys_2v5_adc, doip_line_volt, hs_tempreture);
        }
    } 
}

/** 
******************************************************************************** 
 * @name  show_com_status_msg
 * @deprecated print com status message data
 * @param pac       [in] struct com_status* --> com_packet struct.
 * @param g_channel [in] Send message to specified channel.
 * @return   NA
 * @remarks  Important: . 
 ******************************************************************************** 
*/
void show_com_status_msg(struct com_status* pac, string g_channel)
{
    uint8_t ch_up = 0;
    uint8_t ch_low = 0;
    char msg_type[10];
    uint32_t bus_loader;
    uint32_t time_interval;
    uint32_t bus_event_count;
    uint32_t rx_packet_count;
    uint32_t tx_packet_count;
    time_t t_stamp = (time_t)pac->timestamp;
    get_format_time_st(t_stamp, (char *)time_str);
    /* channel 1~16 , get ch_up and ch_low value. */ 
    char strch[64] = {0};
    strcpy(strch, g_channel.c_str()); 
    sscanf(strch, "%hhu~%hhu", &ch_low, &ch_up);
    if (ch_up == 0) ch_up = ch_low;  
    if (((pac->channel & 0xC0) >> 6) == 0x00) strcpy(msg_type, "can");
    if (((pac->channel & 0xC0) >> 6) == 0x01) strcpy(msg_type, "lin");  
    time_interval   = (pac->data[3]<<24) + (pac->data[2]<<16) + (pac->data[1]<<8) + (pac->data[0]);
    bus_event_count = (pac->data[7]<<24) + (pac->data[6]<<16) + (pac->data[5]<<8) + (pac->data[4]);
    rx_packet_count = (pac->data[11]<<24) + (pac->data[10]<<16) + (pac->data[9]<<8) + (pac->data[8]);
    tx_packet_count = (pac->data[15]<<24) + (pac->data[14]<<16) + (pac->data[13]<<8) + (pac->data[12]);
    bus_loader      = (pac->data[19]<<24) + (pac->data[18]<<16) + (pac->data[17]<<8) + (pac->data[16]);
    for (uint8_t ch = ch_low; ch <= ch_up; ++ch)
    {
        if (((pac->channel & 0x0F) == ch) || ((ch_low == 0) && (ch_up == 0 )))
        {
            printf("%lu\t%s\t%s\t%hhu\t%u\t%u\t%u\t%u\t%f\n", 
                    ++g_rx_count, time_str, msg_type, (pac->channel & 0x0F), time_interval, bus_event_count, rx_packet_count, tx_packet_count, bus_loader*0.0001);
        }
    }
}

/** 
******************************************************************************** 
 * @name  show_com_event_msg
 * @deprecated print com event message data
 * @param pac       [in] struct com_packet* --> com_packet struct.
 * @param g_channel [in] Send message to specified channel.
 * @return   NA
 * @remarks  Important: . 
 ******************************************************************************** 
*/
void show_com_event_msg(struct com_event* pac, string g_channel)
{
    uint8_t ch_up = 0;
    uint8_t ch_low = 0;    
    char msg_type[10];
    uint8_t REC;
    uint8_t TEC;
    uint8_t can_controller_sate;
    uint8_t can_bus_state;
    time_t t_stamp = (time_t)pac->timestamp;
    get_format_time_st(t_stamp, (char *)time_str);
    /* channel 1~16 , get ch_up and ch_low value. */ 
    char strch[64] = {0};
    strcpy(strch, g_channel.c_str()); 
    sscanf(strch, "%hhu~%hhu", &ch_low, &ch_up);
    if (ch_up == 0) ch_up = ch_low;      
    if (((pac->channel & 0xC0) >> 6) == 0x00) strcpy(msg_type, "can");
    if (((pac->channel & 0xC0) >> 6) == 0x01) strcpy(msg_type, "lin");  
    can_controller_sate = pac->data[0]; /* 0 -disable; 1 -normal; 2 -listen only */
    can_bus_state = pac->data[1];   /* 0 -normal; 1 -active error; 2 -passive error; 3 -bus off */
    REC = pac->data[2];
    TEC = pac->data[3];
    for (uint8_t ch = ch_low; ch <= ch_up; ++ch)
    {
        if (((pac->channel & 0x0F) == ch) || ((ch_low == 0) && (ch_up == 0 )))
        {
            printf("%lu\t%s\t\t\t%s\t%hhu\t%hhu\t%hhu\t%hhu\t%hhu\t%hhu\n", 
                ++g_rx_count, time_str, msg_type, pac->channel & 0x1f, can_controller_sate, can_bus_state, REC, TEC,pac->data[4]);
        }
    }
}

/** 
******************************************************************************** 
 * @name  show_gps_data_msg
 * @deprecated print GPS data
 * @param pac       [in] struct gps_data* 
 * @return   NA
 * @remarks  Important: . 
 ******************************************************************************** 
*/
void show_gps_data_msg(struct gps_data* pac)
{
    time_t t_stamp = (time_t)pac->timestamp;
    get_format_time_st(t_stamp, (char *)time_str);

    printf("-------------------------------------------------------------\n");
    printf("rx_count:   %lu\n", ++g_rx_count);
    printf("timestamp:  %s\n", time_str);
    printf("gpstime:    %d\n", pac->gpstime);
    printf("state:      %hhu\n", pac->state);
    printf("satelltes:  %hhu\n", pac->satelltes);
    printf("nsdir:      %hhu\n", pac->nsdir);
    printf("ewdir:      %hhu\n", pac->ewdir);
    printf("latitude:   %f\n", pac->latitude);
    printf("longtitude: %f\n", pac->longtitude);
    printf("altitude:   %f\n", pac->altitude);
    printf("speed:      %f\n", pac->speed);
    printf("direction:  %f\n", pac->direction);
}

/** 
******************************************************************************** 
 * @name  show_state_data_msg
 * @deprecated print state data
 * @param pac       [in] struct rmu_state* 
 * @return   NA
 * @remarks  Important: . 
 ******************************************************************************** 
*/
void show_state_data_msg(struct rmu_state* pac)
{
    time_t t_stamp = (time_t)pac->timestamp;
    get_format_time_st(t_stamp, (char *)time_str);
    printf("-------------------------------------------------------------\n");
    printf("timestamp:        %s\n", time_str);
    printf("wakesrc:          %02X (0x0-IGN on; 0x1-CAN; 0x2-RTC; 0x3-OBD up; 0x4-OBD down; 0x5-NoSleepWkup)\n", pac->wakesrc);
    printf("canmode:          %02X (0x0-Normal; 0x1-Listening; 0x2-Always Listening)\n", pac->canmode);
    printf("distate:          %02X\n", pac->distate);
    printf("dostate:          %02X\n", pac->dostate);
    printf("obdvolt:          %hd mV\n", pac->obdvolt);
    printf("batteryvolt:      %hd mV\n", pac->batteryvolt);
    printf("ledstate:         %lX\n", pac->ledstate);
    printf("hs_temp:          %hd 0.1C°\n", pac->hs_temp);
    printf("ls_temp:          %hd 0.1C°\n", pac->ls_temp);
    printf("batterytemp:      %hd 0.1C°\n", pac->batterytemp);
    printf("doiplinevolt:     %hd mV\n", pac->doiplinevolt);
    printf("hsd_adc[0]:       %hd mV\n", pac->hsd_adc[0]);
    printf("hsd_adc[1]:       %hd mV\n", pac->hsd_adc[1]);
    printf("lsd_adc[0]:       %hd mV\n", pac->lsd_adc[0]);
    printf("lsd_adc[1]:       %hd mV\n", pac->lsd_adc[1]);
    printf("ai:               %hd mV\n", pac->ai);
    printf("ignstate:         %hhu (1-IGN on; 0-IGN off)\n", pac->ignstate);
    printf("battchargecount   %hhu count\n", pac->battchargecount);
    printf("sysstate          %hhu (0-Pow on init; 1-Running)\n", pac->sysstate);
    printf("battoutstatus     %hhu (1-Battery out; 0-Battery in)\n", pac->battoutstatus);
    printf("battchargestatus  %hhu (1-Battery charging; 0-Battery is not charging)\n", pac->battchargestatus);
    printf("obdoutstate:      %hhu (1-OBD out; 0-OBD in)\n",pac->obd_outstate);
    printf("otastate:         %hhu (1-K3 in OTA; 0-K3 is not in OTA)\n",pac->otastate);
    printf("k3_crcerr:        %hhu (1-K3 crc error; 0-K3 is not crc error)\n",pac->k3_crcerr);
    printf("a53appver:        %s\n",pac->a53appver);
    printf("m7fwver:          %s\n",pac->m7fwver);
    printf("k3fwver:          %s\n",pac->k3fwver);
    printf("\n");
}
/** 
******************************************************************************** 
 * @name  get_state_data_msg
 * @deprecated print state data
 * @param pac       [in] struct rmu_state* 
 * @return   NA
 * @remarks  Important: . 
 ******************************************************************************** 
*/
void get_state_data_msg(struct rmu_state* pac)
{
    g_state.ignState = pac->ignstate;
}


/** 
******************************************************************************** 
 * @name  msg_tx_canmsg_test
 * @deprecated Send can message
 * @param handle    [in] embus connect handle.
 * @param channel   [in] Send message to specified channel 1~16.
 * @param loop      [in] Number of sent messages.
 * @param interval  [in] Send message cycle (ms).
 * @param frame     [in] Send message content com_frame.
 * @return  Send message flag
 * @remarks Important: . 
 ******************************************************************************** 
*/
int msg_tx_canmsg_test(embus_handle_t* handle, string channel, int loop, int interval, string frame)
{
    int ret = 0;
    int sepnum = 0;
    uint8_t ch_up = 0;
    uint8_t ch_low = 0;  
    uint8_t type[8];
    uint8_t ide_flag[4];
    struct em_request s_reqset;
    
    char local_str[64] = {0};
    char strc[128] = {0};
    uint32_t canid = 0;
    
    for(uint64_t i = 0; i < frame.length(); ++i)
    {
        if (frame[i] == '#') sepnum++;
    } 
    strcpy(strc, frame.c_str()); 
    if (sepnum == 4)
        sscanf(strc, "%x#%[a-z]#%[a-z]#%hu#%s", &canid, type, ide_flag, &s_txcan_frame[0].dlc, local_str);       
    else
        sscanf(strc, "%x#%[a-z]#%hu#%s", &canid, type, &s_txcan_frame[0].dlc, local_str);
    StringtoHex(local_str, s_txcan_frame[0].data); 
    /* channel 1~16 , get ch_up and ch_low value. */ 
    char strch[64] = {0};
    strcpy(strch, channel.c_str()); 
    sscanf(strch, "%hhu~%hhu", &ch_low, &ch_up);
    if (ch_up == 0) ch_up = ch_low;
    
    int txnum = ch_up - ch_low + 1;
    uint8_t ch = ch_low;

    s_reqset.command = 0x01;
    s_reqset.protocol = 0x01;
    s_reqset.channel = 0;
    s_reqset.param = txnum; 
    s_txcan_frame[0].timestamp = 0;

    /* ide flag */
    if (strcmp((char *)ide_flag, "s") == 0) 
        s_txcan_frame[0].id = canid + 0x00000000; /* + 0b0000 0000 0000 0000 0000 0000 0000 0000 */
    else if (strcmp((char *)ide_flag, "e") == 0)
        s_txcan_frame[0].id = canid + 0x80000000; /* + 0b1000 0000 0000 0000 0000 0000 0000 0000 */
    else 
        s_txcan_frame[0].id = canid;

    if (strcmp((char *)type, "std") == 0)
    {
        s_txcan_frame[0].channel = ch + 0x20 + 0x00; /* + 0b 00 1 0 0000 + 0b 00 000000 */
        s_txcan_frame[0].dlc = 8;
    }
    else if (strcmp((char *)type, "fd") == 0)
    {
        s_txcan_frame[0].channel = ch + 0x20 + 0x40; /* + 0b 00 1 0 0000 + 0b 01 000000 */
        if (s_txcan_frame[0].dlc > 8)    
            s_txcan_frame[0].flag = 0x02;
        else
            s_txcan_frame[0].flag = 0;
    }
    else if (strcmp((char *)type, "lin") == 0)
    {
        s_txcan_frame[0].channel = ch + 0x20 + 0x80; /* + 0b 00 1 0 0000 + 0b 10 000000 */ 
    }
    int j = 0;
    printf("send canid = %x, channel = %x\n",s_txcan_frame[0].id,ch); 
    for (ch = ch_low; ch < ch_up; ++ch)
    {    
        memcpy(&s_txcan_frame[++j],&s_txcan_frame[0],sizeof(struct com_packet)); /* struct -> uint8_t *  */     
        s_txcan_frame[j].channel = s_txcan_frame[0].channel + j;
        s_txcan_frame[j].id += j;
        printf("send canid = %x, channel = %x\n",s_txcan_frame[j].id,(ch+1));     
    }
    memcpy(s_reqset.data, (uint8_t *)(&s_txcan_frame[0]), sizeof(struct com_packet)*txnum); /* struct -> uint8_t *  */
    for (int i = 0; i < loop; ++i)
    {
        ret = embus_msg_send(handle, EMBUS_BMR_CTRL_ID, (uint8_t *)(&s_reqset), sizeof(struct com_packet)*txnum + 4);
        std::this_thread::sleep_for(std::chrono::milliseconds(interval));
    }
    // free(arr_can_frame);
    return ret; 
}

/** 
******************************************************************************** 
 * @name  msg_tx_canconfig_test
 * @deprecated can/fd communication interface parameter configuration
 * @param handle     [in] embus handle.
 * @param channel    [in] can/canfd specifies the configuration channel 1~ 16.
 * @param baudrate   [in] Configure can baud rate 250kbps 500kbps...
 * @param dbaudrate  [in] Configure canfd baud rate 1Mbps 2Mbps...
 * @param sample     [in] Configure can sample rate 75% 80%...
 * @param dsample    [in] Configure canfd sample rate 80%...
 * @return  Send message flag
 * @remarks Important: . 
 ******************************************************************************** 
*/
int msg_tx_canconfig_test(embus_handle_t* handle, string channel, uint32_t baudrate, uint32_t dbaudrate, uint8_t sample, uint8_t dsample)
{
    int ret;
    uint8_t ch_up = 0;
    uint8_t ch_low = 0; 
    struct can_param s_cparam;
    struct em_request s_reqset;
    /* channel 1~16 , get ch_up and ch_low value. */ 
    char strch[64] = {0};
    strcpy(strch, channel.c_str()); 
    sscanf(strch, "%hhu~%hhu", &ch_low, &ch_up);
    if (ch_up == 0) ch_up = ch_low;  
    for (uint8_t ch = ch_low; ch <= ch_up; ++ch)
    {   
        s_reqset.command = 0x02;
        s_reqset.protocol = 0x01;
        s_reqset.channel = ch;
        s_reqset.param = 2; /* 2-can-setting */ 
        /*1. can/canfd baudrate (80MHz)
         * can: 250k 80%
         * canfd: 1M 80% */
        if (baudrate == 250000 && sample == 80 && dbaudrate == 1000000 &&  dsample == 80) {
            s_cparam.norm_div         = 2;
            s_cparam.norm_prop_seg    = 77;
            s_cparam.norm_phase_seg1  = 50;
            s_cparam.norm_phase_seg2  = 32;
            s_cparam.fast_div         = 2;
            s_cparam.fast_prop_seg    = 15;
            s_cparam.fast_phase_seg1  = 16;
            s_cparam.fast_phase_seg2  = 8;
            s_cparam.norm_sjw     = 1;
            s_cparam.fast_sjw     = 1;  
            s_cparam.reserved0 		= 0;
            s_cparam.reserved1       = 0;                   
        }
        /*2. can/canfd baudrate (80MHz)
         * can: 500k 80%
         * canfd: 1M 80% */
        else if (baudrate == 500000 && sample == 80 && dbaudrate == 1000000 &&  dsample == 80) {
            s_cparam.norm_div         = 2;
            s_cparam.norm_prop_seg    = 33;
            s_cparam.norm_phase_seg1  = 30;
            s_cparam.norm_phase_seg2  = 16;
            s_cparam.fast_div         = 2;
            s_cparam.fast_prop_seg    = 15;
            s_cparam.fast_phase_seg1  = 16;
            s_cparam.fast_phase_seg2  = 8;
            s_cparam.norm_sjw         = 1;
            s_cparam.fast_sjw         = 1;  
            s_cparam.reserved0 		  = 0;
            s_cparam.reserved1        = 0;                        
        }
        /*3. can/canfd baudrate (80MHz)
         * can: 500k 75%
         * canfd: 2M 80% */ 
        else if (baudrate == 500000 && sample == 75 && dbaudrate == 2000000 &&  dsample == 80) {
            s_cparam.norm_div         = 2;
            s_cparam.norm_prop_seg    = 33;
            s_cparam.norm_phase_seg1  = 26;
            s_cparam.norm_phase_seg2  = 20;
            s_cparam.fast_div         = 2;
            s_cparam.fast_prop_seg    = 8;
            s_cparam.fast_phase_seg1  = 7;
            s_cparam.fast_phase_seg2  = 4;
            s_cparam.norm_sjw         = 1;
            s_cparam.fast_sjw         = 1;
            s_cparam.reserved0 		  = 0;
            s_cparam.reserved1        = 0;           
        }
        else {/* 4. not support */
            s_cparam.norm_div         = 0;
            s_cparam.norm_prop_seg    = 0;
            s_cparam.norm_phase_seg1  = 0;
            s_cparam.norm_phase_seg2  = 0;
            s_cparam.fast_div         = 0;
            s_cparam.fast_prop_seg    = 0;
            s_cparam.fast_phase_seg1  = 0;
            s_cparam.fast_phase_seg2  = 0;
            s_cparam.norm_sjw         = 0;
            s_cparam.fast_sjw         = 0;
            s_cparam.reserved0 		  = 0;
            s_cparam.reserved1        = 0;       
        }    
        printf("config: channel=%hhu baudrate=%u dbaudrate=%u sample=%hhu dsample=%hhu\n", ch, baudrate, dbaudrate, sample, dsample);
        uint8_t* cdata = (uint8_t *)(&s_cparam);
        memcpy(s_reqset.data, cdata, 20);
        uint8_t* txdata = (uint8_t *)(&s_reqset);
        ret = embus_msg_send(handle, EMBUS_VCI_REQ_ID, txdata, 24);
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    return ret;
}

/** 
******************************************************************************** 
 * @name  msg_tx_cansetmode_test
 * @deprecated can/fd communication interface mode configuration
 * @param handle    [in] embus descriptor.
 * @param channel   [in] can/canfd specifies the configuration channel 1~16.
 * @param mode      [in] can/canfd mode, 0 -disable; 1 -normal; 2 -listen only.
 * @return  send message flag.
 * @remarks Important: . 
 ******************************************************************************** 
*/
int msg_tx_cansetmode_test(embus_handle_t* handle, string channel, uint8_t mode)
{
    int ret;
    uint8_t ch_up = 0;
    uint8_t ch_low = 0;
    struct em_request s_reqset;
    /* channel 1~16 , get ch_up and ch_low value. */ 
    char strch[64] = {0};
    strcpy(strch, channel.c_str()); 
    sscanf(strch, "%hhu~%hhu", &ch_low, &ch_up);
    if (ch_up == 0) ch_up = ch_low;  
    for (uint8_t ch = ch_low; ch <= ch_up; ++ch)
    { 
        s_reqset.command   = 0x02;
        s_reqset.protocol = 0x01;
        s_reqset.channel  = ch;
        s_reqset.param    = 3;    /* 3-can-mode */ 
        s_reqset.data[0]  = mode;
        uint8_t* txdata = (uint8_t *)(&s_reqset);
        printf("can set mode : %hhu\n", mode);
        ret = embus_msg_send(handle, EMBUS_VCI_REQ_ID, txdata, 5); 
    }
    return ret;
}

/** 
******************************************************************************** 
 * @name  msg_tx_linconfig_test
 * @deprecated lin communication interface parameter configuration
 * @param handle     [in] embus handle.
 * @param channel    [in] lin specifies the configuration channel 1~2.
 * @param rate       [in] Configure lin baud rate 4800 9600.
 * @return  Send message flag
 * @remarks Important: . 
 ******************************************************************************** 
*/
int msg_tx_linconfig_test(embus_handle_t* handle, string channel, uint16_t rate)
{   
    int ret;
    uint8_t ch_up = 0;
    uint8_t ch_low = 0;
    struct em_request s_reqset;
    /* channel 1~16 , get ch_up and ch_low value. */ 
    char strch[64] = {0};
    strcpy(strch, channel.c_str()); 
    sscanf(strch, "%hhu~%hhu", &ch_low, &ch_up);
    if (ch_up == 0) ch_up = ch_low;  
    for (uint8_t ch = ch_low; ch <= ch_up; ++ch)
    { 
        s_reqset.command   = 0x02;
        s_reqset.protocol = 0x02;
        s_reqset.channel  = ch;    /* bit 0~6 (1~2) */
        s_reqset.param   = 1;       /* 1-lin-setting */
        s_reqset.data[0] = rate & 0xFF;   /* data[0~1]: lin's communication rate */
        s_reqset.data[1] = rate >> 8;
        s_reqset.data[2] = 0x00;          /* reserved */
        s_reqset.data[3] = 0x00;
        printf("lin config: channel=%hhu rate=%u \n", ch, rate);
        uint8_t* txdata = (uint8_t *)(&s_reqset);
        ret = embus_msg_send(handle, EMBUS_VCI_REQ_ID, txdata, 8);
    }     
    return ret;
}

/** 
******************************************************************************** 
 * @name  msg_tx_linsetmode_test
 * @deprecated lin communication interface mode configuration
 * @param handle    [in] embus handle.
 * @param channel   [in] lin specifies the configuration channel 1~2.
 * @param mode      [in] lin interface mode, 0 -disable; 2 -listen only
 * @return  send message flag
 * @remarks Important: . 
 ******************************************************************************** 
*/
int msg_tx_linsetmode_test(embus_handle_t* handle, string channel, uint8_t mode)
{
    int ret;
    uint8_t ch_up = 0;
    uint8_t ch_low = 0;
    struct em_request s_reqset;
    /* channel 1~16 , get ch_up and ch_low value. */ 
    char strch[64] = {0};
    strcpy(strch, channel.c_str()); 
    sscanf(strch, "%hhu~%hhu", &ch_low, &ch_up);
    if (ch_up == 0) ch_up = ch_low;  
    for (uint8_t ch = ch_low; ch <= ch_up; ++ch)
    { 
        s_reqset.command   = 0x02;
        s_reqset.protocol = 0x02;
        s_reqset.channel  = ch;
        s_reqset.param    = 2; /* 2-lin-mode */
        s_reqset.data[0]  = mode;
        uint8_t* txdata = (uint8_t *)(&s_reqset);
        printf("lin set mode : %hhu\n", mode);
        ret = embus_msg_send(handle, EMBUS_VCI_REQ_ID, txdata, 5); 
    }
    return ret;
}

/** 
******************************************************************************** 
 * @name  msg_tx_DoipPowerOnInitSet_test
 * @deprecated lin communication interface mode configuration
 * @param handle    [in] embus handle.
 * @param channel   [in] lin specifies the configuration channel 1~2.
 * @param mode      [in] lin interface mode, 0 -disable; 2 -listen only
 * @return  send message flag
 * @remarks Important: . 
 ******************************************************************************** 
*/
int msg_tx_DoipPowerOnInitSet_test(embus_handle_t* handle, uint8_t state)
{
    struct em_request s_reqset;
    s_reqset.command   = 0x02;
    s_reqset.protocol = 0x03;  /* DoIP Active Line */
    s_reqset.channel  = 0;
    s_reqset.param    = 0; 
    s_reqset.data[0]  = state;  /* 0 – Low ; 1 - High */
    
    printf("DoipPowerOnInitSet setting message: ");
    if (state == 0)
        printf(" low\n");
    else if (state == 1)
        printf(" high\n");        
    uint8_t* txdata = (uint8_t *)(&s_reqset);
    int ret = embus_msg_send(handle, EMBUS_VCI_REQ_ID, txdata, 5); 
    return ret;
}

/**
********************************************************************************
 * @name  msg_tx_DoipSet_test
 * @deprecated DoIP active line set interface
 * @param handle    [in] embus handle.
 * @param state     [in] 0-low, 1 -high
 * @return  send message flag
 * @remarks Important: .
 ********************************************************************************
*/
int msg_tx_DoipSet_test(embus_handle_t* handle, uint8_t state)
{
    static bool sb_doip_gpio_is_inited = false;
    if(!sb_doip_gpio_is_inited){
        (void)system("echo 39 > /sys/class/gpio/export");
        (void)system("echo out > /sys/class/gpio/gpio39/direction");
        sb_doip_gpio_is_inited = true;
    }

    printf("DoipSet setting message: ");
    if (state == 0){
        (void)system("echo 0 > /sys/class/gpio/gpio39/value");
        printf(" low\n");
    }
    else if (state == 1){
        (void)system("echo 1 > /sys/class/gpio/gpio39/value");
        printf(" high\n");
    }
    return 0;
}

/** 
******************************************************************************** 
 * @name  msg_tx_RTCwakeup_test
 * @deprecated 
 * @param handle    [in] embus handle.
 * @param wktime    [in] RTC wake-up time day:hour:minute:second
 * @param wk_mode   [in] RTC wake-up mode 0 - month  1 - week
 * @return  send message flag
 * @remarks 
 ******************************************************************************** 
*/
int msg_tx_RTCwakeup_test(embus_handle_t* handle, string wk_time, uint8_t wk_mode)
{
    uint8_t day, hour, minute, second;
    struct em_request s_reqset;
    s_reqset.command  = 0x02;
    s_reqset.protocol = 0x04;  /* RTC */
    s_reqset.channel  = 0;
    s_reqset.param    = 0; 
    sscanf(wk_time.c_str(), "%hhu-%hhu:%hhu:%hhu", &day, &hour, &minute, &second);
    s_reqset.data[0]  = second;  
    s_reqset.data[1]  = minute;  
    s_reqset.data[2]  = hour;  
    s_reqset.data[3]  = day;  
    s_reqset.data[4]  = wk_mode;
    
    printf("Set RTC wake up mode: %hhu  time(day hour:minute:second): %hhu-%hhu:%hhu:%hhu \n", 
            s_reqset.data[4], s_reqset.data[3], s_reqset.data[2], s_reqset.data[1], s_reqset.data[0]);       
    uint8_t* txdata = (uint8_t *)(&s_reqset);
    int ret = embus_msg_send(handle, EMBUS_VCI_REQ_ID, txdata, 9); 
    return ret;
}

/** 
******************************************************************************** 
 * @name  msg_tx_DOstate_test
 * @deprecated  control DO output state
 * @param handle    [in] embus handle.
 * @param DOctl     [in] DO enable control, 0 - no control, 1 - control enable;
 * @param DOstate   [in] DO state control, 0 - open, 1 - closed;
 * @return  send message flag
 * @remarks 
 ******************************************************************************** 
*/
int msg_tx_DOstate_test(embus_handle_t* handle, string DOctl, string DOstate)
{
    int ret = 0;
    uint8_t bDO_ctl[4];
    uint8_t bDO_state[4];
    struct em_request s_reqset;
    s_reqset.command  = 0x02;
    s_reqset.protocol = 0x05;   /* DO */
    s_reqset.channel  = 0;
    s_reqset.param    = 0; 
    sscanf(DOctl.c_str(), "%hhu,%hhu,%hhu,%hhu", &bDO_ctl[0], &bDO_ctl[1], &bDO_ctl[2], &bDO_ctl[3]);
    sscanf(DOstate.c_str(), "%hhu,%hhu,%hhu,%hhu", &bDO_state[0], &bDO_state[1], &bDO_state[2], &bDO_state[3]);
    s_reqset.data[0]  = 0x00 | bDO_ctl[0] | (bDO_ctl[1]<<1) | (bDO_ctl[2]<<2) | (bDO_ctl[3]<<3);  
    s_reqset.data[1]  = 0x00 | bDO_state[0] | (bDO_state[1]<<1) | (bDO_state[2]<<2) | (bDO_state[3]<<3);  

    uint8_t* txdata = (uint8_t *)(&s_reqset);
    ret = embus_msg_send(handle, EMBUS_VCI_REQ_ID, txdata, 6); 

    return ret;
}

/** 
******************************************************************************** 
 * @name  msg_tx_LEDctrl_test
 * @deprecated  set LED control output status
 * @param handle      [in] embus handle.
 * @param LEDctl      [in] led control enable, 0 - enable, 1 - disable
 * @param LEDstate    [in] led control options, 0 - extinguished, 1 - slow flash, 2 - flash, 3 - always bright
 * @return  send message flag
 * @remarks 
 ******************************************************************************** 
*/
int msg_tx_LEDctrl_test(embus_handle_t* handle, string LEDctl, string LEDstate)
{
    int ret = 0;
    uint8_t bLED_ctl[18];
    uint8_t bLED_state[18];
    struct em_request s_reqset;
    s_reqset.command  = 0x02;
    s_reqset.protocol = 0x06;  /* LED */
    s_reqset.channel  = 0;
    s_reqset.param    = 0; 
    sscanf(LEDctl.c_str(), "%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu", 
                &bLED_ctl[0], &bLED_ctl[1], &bLED_ctl[2], &bLED_ctl[3], &bLED_ctl[4], &bLED_ctl[5], &bLED_ctl[6], &bLED_ctl[7], &bLED_ctl[8],
                &bLED_ctl[9], &bLED_ctl[10], &bLED_ctl[11], &bLED_ctl[12], &bLED_ctl[13], &bLED_ctl[14], &bLED_ctl[15], &bLED_ctl[16], &bLED_ctl[17]);
    sscanf(LEDstate.c_str(), "%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu,%hhu", 
                &bLED_state[0], &bLED_state[1], &bLED_state[2], &bLED_state[3], &bLED_state[4], &bLED_state[5], &bLED_state[6], &bLED_state[7], &bLED_state[8],
                &bLED_state[9], &bLED_state[10], &bLED_state[11], &bLED_state[12], &bLED_state[13], &bLED_state[14], &bLED_state[15], &bLED_state[16], &bLED_state[17]);

    s_reqset.data[0]  = 0x00 | bLED_ctl[0] | (bLED_ctl[1]<<1) | (bLED_ctl[2]<<2) | (bLED_ctl[3]<<3) | (bLED_ctl[4]<<4) | (bLED_ctl[5]<<5) | (bLED_ctl[6]<<6) | (bLED_ctl[7]<<7);
    s_reqset.data[1]  = 0x00 | bLED_ctl[8] | (bLED_ctl[9]<<1) | (bLED_ctl[10]<<2) | (bLED_ctl[11]<<3) | (bLED_ctl[12]<<4) | (bLED_ctl[13]<<5) | (bLED_ctl[14]<<6) | (bLED_ctl[15]<<7) ;
    s_reqset.data[2]  = 0x00 | bLED_ctl[16] | (bLED_ctl[17]<<1);
    s_reqset.data[3]  = 0x00;
    
    s_reqset.data[4]  = 0x00 | bLED_state[0] | (bLED_state[1]<<2) | (bLED_state[2]<<4) | (bLED_state[3]<<6);
    s_reqset.data[5]  = 0x00 | bLED_state[4] | (bLED_state[5]<<2) | (bLED_state[6]<<4) | (bLED_state[7]<<6);
    s_reqset.data[6]  = 0x00 | bLED_state[8] | (bLED_state[9]<<2) | (bLED_state[10]<<4) | (bLED_state[11]<<6);
    s_reqset.data[7]  = 0x00 | bLED_state[12] | (bLED_state[13]<<2) | (bLED_state[14]<<4) | (bLED_state[15]<<6);
    s_reqset.data[8]  = 0x00 | bLED_state[16] | (bLED_state[17]<<2);
    s_reqset.data[9]  = 0x00;
    s_reqset.data[10] = 0x00;
    s_reqset.data[11] = 0x00;

    uint8_t* txdata = (uint8_t *)(&s_reqset);
    ret = embus_msg_send(handle, EMBUS_VCI_REQ_ID, txdata, 16); 

    return ret;
}

/** 
******************************************************************************** 
 * @name  msg_tx_BUZctrl_test
 * @deprecated  set LED control output status
 * @param handle      [in] embus handle.
 * @param BUZstate    [in] led control options, 0 - close, 1- open, other - time (ms)
 * @return  send message flag
 * @remarks 
 ******************************************************************************** 
*/
int msg_tx_BUZctrl_test(embus_handle_t* handle, uint32_t BUZstate)
{
    int ret = 0;
    struct em_request s_reqset;
    s_reqset.command  = 0x02;
    s_reqset.protocol = 0x07;  /* BUZZER */
    s_reqset.channel  = 0;
    s_reqset.param    = 0; 
    s_reqset.data[0]  = BUZstate & 0xFF;
    s_reqset.data[1]  = (BUZstate >> 8) & 0xFF;
    s_reqset.data[2]  = (BUZstate >> 16) & 0xFF;
    s_reqset.data[3]  = (BUZstate >> 24) & 0xFF;

    uint8_t* txdata = (uint8_t *)(&s_reqset);
    ret = embus_msg_send(handle, EMBUS_VCI_REQ_ID, txdata, 8); 

    return ret;    
}

/** 
******************************************************************************** 
 * @name  msg_tx_sleep_enable_test
 * @deprecated  sleep enable setting
 * @param handle    [in] embus handle.
 * @param mode      [in] 0xFF, enable sleep
 * @return  send message flag
 * @remarks 
 ******************************************************************************** 
*/
int msg_tx_sleep_enable_test(embus_handle_t* handle, string mode)
{
    struct em_request s_reqset;
    s_reqset.command   = 0x02;
    s_reqset.protocol = 0x0A;           /* system sleep mode */
    s_reqset.channel  = 0;
    s_reqset.param    = 0; 
    sscanf(mode.c_str(), "0x%hhx", &s_reqset.data[0]);
    
    printf("Set sleep mode: 0x%02X ", s_reqset.data[0]);
    if (s_reqset.data[0] == 0xFF)
        printf("enable\n");
    else
        printf("disable\n");   

    uint8_t* txdata = (uint8_t *)(&s_reqset);
    int ret = embus_msg_send(handle, EMBUS_DORMANCY_CMD_ID, txdata, 5); 
    return ret;
}

/** 
******************************************************************************** 
 * @name  msg_tx_ota_test
 * @deprecated  sleep enable setting
 * @param handle    [in] embus handle.
 * @param req      [in] request
 * @return  send message flag
 * @remarks 
 ******************************************************************************** 
*/
int msg_tx_ota_test(embus_handle_t* handle, uint8_t req)
{
    struct em_request s_reqset;
    s_reqset.command   = 0x02;
    s_reqset.protocol = 0x55;           /* system sleep mode */
    s_reqset.channel  = 0;
    s_reqset.param    = 0;

    s_reqset.data[0] = req;

    uint8_t* txdata = (uint8_t *)(&s_reqset);
    int ret = embus_msg_send(handle, EMBUS_VCI_REQ_ID, txdata, 5); 
    return ret;
}

/** 
******************************************************************************** 
 * @name  get_ign_state
 * @deprecated  na
 * @return  ign on state: 1-on, 0-off, 0xff-un_get
 * @remarks 
 ******************************************************************************** 
*/
int get_ign_state()
{
    return g_state.ignState;
}